﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using ERP_RORZE_SYSTEM;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
using System.Threading;

namespace ERP_RORZE_DLL.Assembly
{
    public partial class frm_Add_F4CheckRR713 : Form
    {
        private INISettings mySetting;
        private SqlConnection myConnection;

        private DataSet ds;
        private DataViewManager dsView;

        private Dictionary<string, string> required_list;

        public frm_Add_F4CheckRR713()
        {
            InitializeComponent();
            this.mySetting = new INISettings();
            this.myConnection = new SqlConnection(mySetting.SQLConnectionString);
            this.ds = new DataSet("RobotOrderDetail");

            this.required_list = new Dictionary<string, string>() { { "serialNo", "Serial No" }, { "writtenBy", "Written By" },
            { "checkBy", "Check By"}, {"inputDate", "Date"}, {"wiringDiagram", "Wiring Diagram"}, {"A1_RC234", "A1 RC234"}, 
            {"A1_RC234Ver", "A1 RC234 Ver"}, {"A1_RC234No", "A1 RC234 No"}, {"A1_RD026", "A1 RD026"}, {"A1_Run", "A1 Run"},
            {"A1_Stop", "A1 Stop"}, {"A2_RC234", "A2 RC234"}, {"A2_RC234Ver", "A2 RC234 Ver"}, {"A2_RC234No", "A2 RC234 No"}, 
            {"A2_RD026", "A2 RD026"}, {"A2_Run", "A2 Run"}, {"A2_Stop", "A2 Stop"}, {"R_RC234", "R RC234"},
            {"R_RC234Ver", "R RC234 Ver"}, {"R_RC234No", "R RC234 No"}, {"Rot_RD026", "Rot RD026"}, {"Rot_Run", "Rot Run"},
            {"Rot_Stop", "Rot Stop"}, {"Z_RC234", "Z RC234"}, {"Z_RC234Ver", "Z RC234 Ver"}, {"Z_RC234No", "Z RC234 No"},
            {"Z_RD026", "Z RD026"}, {"Z_Run", "Z Run"}, {"Z_Stop", "Z Stop"}, {"RC204", "RC204"}, {"RC204Ver", "RC204 Ver"},
            {"RC204No", "RC204 No"}, {"A1_R022A", "A1-R022A"}, {"A2_R022A", "A2-R022A"}};

            LoadSerialList();
            LoadStaffList();
            LoadDefaultData();            
        }

        ////////////////////////////////////////////////////
        /// Load Serial List
        ////////////////////////////////////////////////////
        private void LoadSerialList()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo], [CompID] FROM [231_RobotOrderDetail]"
                    + " WHERE(FinishedDate > DateAdd(dd, 0, DateDiff(dd, 0, GETDATE())) - 500) AND [SerialNo] NOT IN"
                    + " (SELECT [SerialNo] COLLATE Vietnamese_CI_AS FROM [231_UnitRR713])";
                SqlDataAdapter da1 = new SqlDataAdapter();
                da1.SelectCommand = cmd_sql;
                da1.TableMappings.Add("Table", "RobotOrderDetail");
                da1.Fill(ds);
                dsView = ds.DefaultViewManager;
                cmd_sql.Dispose();
                myConnection.Close();

                this.serialNo.DataSource = dsView;
                this.serialNo.DisplayMember = "RobotOrderDetail.SerialNo";
                this.serialNo.ValueMember = "RobotOrderDetail.SerialNo";
                this.serialNo.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.partNo.DataBindings.Add("Text", dsView, "RobotOrderDetail.CompID");
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Load Staff List
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadStaffList()
        {
            try
            {
                myConnection.Open();
                SqlCommand sql_cmd = myConnection.CreateCommand();
                sql_cmd.CommandText = "SELECT StaffID, StaffName FROM [222_Staff] WHERE StopDate IS NULL ORDER BY StaffID";
                SqlDataReader dataReader = sql_cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    string[] fullname = dataReader["StaffName"].ToString().Trim().Split(' ');

                    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
                    TextInfo textInfo = cultureInfo.TextInfo;
                    string txt = textInfo.ToTitleCase(fullname[fullname.Length - 1].ToLower());

                    item.Text = txt + dataReader["StaffID"].ToString().Trim();
                    //item.Text = dataReader["StaffID"].ToString().Trim() + " - " + dataReader["StaffName"].ToString().Trim();
                    item.Value = dataReader["StaffID"].ToString().Trim();

                    this.writtenBy.Properties.Items.Add(item);
                    this.checkBy.Properties.Items.Add(item);
                }
                dataReader.Close();
                sql_cmd.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }

        ////////////////////////////////////////////////////
        /// Load Default Data
        ////////////////////////////////////////////////////
        private void LoadDefaultData()
        {
            try
            {
                myConnection.Open();
                SqlCommand cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Wiring Daiagram]"
                    + " FROM [231_UnitRR713]";
                SqlDataReader dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Wiring Daiagram"].ToString().Trim();
                    item.Text = dataReader["Wiring Daiagram"].ToString().Trim();

                    this.wiringDiagram.Properties.Items.Add(item);
                }
                dataReader.Close();                
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A1 RC234 Ver]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A1 RC234 Ver"].ToString().Trim();
                    item.Text = dataReader["A1 RC234 Ver"].ToString().Trim();

                    this.A1_RC234Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A1 RC234 No]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A1 RC234 No"].ToString().Trim();
                    item.Text = dataReader["A1 RC234 No"].ToString().Trim();

                    this.A1_RC234No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A1 run]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A1 run"].ToString().Trim();
                    item.Text = dataReader["A1 run"].ToString().Trim();

                    this.A1_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A1 stop]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A1 stop"].ToString().Trim();
                    item.Text = dataReader["A1 stop"].ToString().Trim();

                    this.A1_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //A2
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A2 RC234 Ver]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A2 RC234 Ver"].ToString().Trim();
                    item.Text = dataReader["A2 RC234 Ver"].ToString().Trim();

                    this.A2_RC234Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A2 RC234 No]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A2 RC234 No"].ToString().Trim();
                    item.Text = dataReader["A2 RC234 No"].ToString().Trim();

                    this.A2_RC234No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A2 run]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A2 run"].ToString().Trim();
                    item.Text = dataReader["A2 run"].ToString().Trim();

                    this.A2_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [A2 stop]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["A2 stop"].ToString().Trim();
                    item.Text = dataReader["A2 stop"].ToString().Trim();

                    this.A2_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //R
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [R RC234 Ver]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["R RC234 Ver"].ToString().Trim();
                    item.Text = dataReader["R RC234 Ver"].ToString().Trim();

                    this.R_RC234Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [R RC234 No]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["R RC234 No"].ToString().Trim();
                    item.Text = dataReader["R RC234 No"].ToString().Trim();

                    this.R_RC234No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Rot run]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Rot run"].ToString().Trim();
                    item.Text = dataReader["Rot run"].ToString().Trim();

                    this.Rot_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Rot stop]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Rot stop"].ToString().Trim();
                    item.Text = dataReader["Rot stop"].ToString().Trim();

                    this.Rot_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                //Z
                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z RC234 Ver]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z RC234 Ver"].ToString().Trim();
                    item.Text = dataReader["Z RC234 Ver"].ToString().Trim();

                    this.Z_RC234Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z RC234 No]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z RC234 No"].ToString().Trim();
                    item.Text = dataReader["Z RC234 No"].ToString().Trim();

                    this.Z_RC234No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z run]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z run"].ToString().Trim();
                    item.Text = dataReader["Z run"].ToString().Trim();

                    this.Z_Run.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [Z stop]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["Z stop"].ToString().Trim();
                    item.Text = dataReader["Z stop"].ToString().Trim();

                    this.Z_Stop.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RC204 Ver]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RC204 Ver"].ToString().Trim();
                    item.Text = dataReader["RC204 Ver"].ToString().Trim();

                    this.RC204Ver.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT DISTINCT [RC204 No]"
                    + " FROM [231_UnitRR713]";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["RC204 No"].ToString().Trim();
                    item.Text = dataReader["RC204 No"].ToString().Trim();

                    this.RC204No.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();

                cmd_sql = myConnection.CreateCommand();
                cmd_sql.CommandText = "SELECT [SerialNo] FROM [231_F5Detail]"
                    + " WHERE ([FinishDate] > DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE())) - 500)";
                dataReader = cmd_sql.ExecuteReader();
                while (dataReader.Read())
                {
                    ComboboxItem item = new ComboboxItem();
                    item.Value = dataReader["SerialNo"].ToString().Trim();
                    item.Text = dataReader["SerialNo"].ToString().Trim();

                    this.A1_RC234.Properties.Items.Add(item);
                    this.A1_RD026.Properties.Items.Add(item);
                    this.A2_RC234.Properties.Items.Add(item);                    
                    this.A2_RD026.Properties.Items.Add(item);
                    this.R_RC234.Properties.Items.Add(item);                    
                    this.Rot_RD026.Properties.Items.Add(item);
                    this.Z_RC234.Properties.Items.Add(item);
                    this.Z_RD026.Properties.Items.Add(item);
                    this.RC204.Properties.Items.Add(item);
                    this.A1_R022A.Properties.Items.Add(item);
                    this.A2_R022A.Properties.Items.Add(item);
                }
                dataReader.Close();
                cmd_sql.Dispose();
                myConnection.Close();
            }
            catch (Exception ex)
            {
                if (myConnection.State == ConnectionState.Open)
                {
                    myConnection.Close();
                }
                MessageBox.Show(ex.Message);
            }
        }
        
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        
        ///////////////////////////
        /// When Click Add Button
        ///////////////////////////        
        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string error = null;

            if (this.serialNo.SelectedIndex < 0)
            {
                error = "Please select Serial No.";
            }

            if (string.IsNullOrEmpty(error))
            {
                foreach (Control c in this.Controls)
                {
                    if (c is System.Windows.Forms.ComboBox)
                    {
                        if ((((System.Windows.Forms.ComboBox)c).SelectedIndex < 0) && string.IsNullOrEmpty(((System.Windows.Forms.ComboBox)c).Text))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.ComboBox)c).Name))
                            {
                                error = "Please select " + required_list[((System.Windows.Forms.ComboBox)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.ComboBoxEdit)
                    {
                        if ((((DevExpress.XtraEditors.ComboBoxEdit)c).SelectedIndex < 0) && string.IsNullOrEmpty(((DevExpress.XtraEditors.ComboBoxEdit)c).Text))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)c).Name))
                            {
                                error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)c).Name];
                            }
                        }
                    }

                    if (c is DevExpress.XtraEditors.TextEdit)
                    {
                        if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)c).Name))
                            {
                                error = "Please enter " + required_list[((DevExpress.XtraEditors.TextEdit)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.TextBox)
                    {
                        if (string.IsNullOrEmpty(((System.Windows.Forms.TextBox)c).Text.Trim()))
                        {
                            if (required_list.ContainsKey(((System.Windows.Forms.TextBox)c).Name))
                            {
                                error = "Please enter " + required_list[((System.Windows.Forms.TextBox)c).Name];
                            }
                        }
                    }

                    if (c is System.Windows.Forms.GroupBox)
                    {
                        foreach (Control sub_c in c.Controls)
                        {
                            if (sub_c is DevExpress.XtraEditors.TextEdit)
                            {
                                if (string.IsNullOrEmpty(((DevExpress.XtraEditors.TextEdit)sub_c).Text.Trim()))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.TextEdit)sub_c).Name))
                                    {
                                        error = "Plaase enter " + required_list[((DevExpress.XtraEditors.TextEdit)sub_c).Name];
                                    }
                                }
                            }

                            if (sub_c is DevExpress.XtraEditors.ComboBoxEdit)
                            {
                                if ((((DevExpress.XtraEditors.ComboBoxEdit)sub_c).SelectedIndex < 0) && string.IsNullOrEmpty(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Text))
                                {
                                    if (required_list.ContainsKey(((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name))
                                    {
                                        error = "Please select " + required_list[((DevExpress.XtraEditors.ComboBoxEdit)sub_c).Name];
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (string.IsNullOrEmpty(error))
            {
                DateTime date = DateTime.Parse(this.inputDate.EditValue.ToString());
                try
                {
                    myConnection.Open();
                    SqlCommand sql_cmd = myConnection.CreateCommand();
                    sql_cmd.CommandText = "INSERT INTO [231_UnitRR713] ([SerialNo], [Written], [Date], [Checked],"
                        + " [Wiring Daiagram], [A1 RC234], [A1 RC234 Ver], [A1 RC234 No], [A1 RD026], [A1 run], [A1 stop],"
                        + " [A2 RC234], [A2 RC234 Ver], [A2 RC234 No], [A2 RD026], [A2 run], [A2 stop], [R RC234],"
                        + " [R RC234 Ver], [R RC234 No], [Rot RD026], [Rot run], [Rot stop], [Z RC234], [Z RC234 Ver],"
                        + " [Z RC234 No], [Z RD026], [Z run], [Z stop], [RC204], [RC204 Ver], [RC204 No], [A1-R022A],"
                        + " [A2-R022A], [A1-OH], [A1-OL], [A1-Q], [A1-OX], [A1-OC], [A1-OS], [A1-0S], [A2-OH],"
                        + " [A2-OL], [A2-Q], [A2-OX], [A2-OC], [A2-OS], [A2-0S], [R-OH], [R-OL], [R-Q], [R-OX],"
                        + " [R-OC], [R-OS], [R-0S], [Z-OH], [Z-OL], [Z-Q], [Z-OX], [Z-OC], [Z-OS], [Z-0S], [Remark])"
                        + " VALUES ('" + this.serialNo.SelectedValue.ToString() + "', '" + this.writtenBy.Text.Trim() + "',"
                        + " @date, '" + this.checkBy.Text.Trim() + "', '" + this.wiringDiagram.Text.Trim() + "',"
                        + " '" + this.A1_RC234.Text.Trim() + "', '" + this.A1_RC234Ver.Text.Trim() + "',"
                        + " '" + this.A1_RC234No.Text.Trim() + "', '" + this.A1_RD026.Text.Trim() + "',"
                        + " '" + this.A1_Run.Text.Trim() + "', '" + this.A1_Stop.Text.Trim() + "',"
                        + " '" + this.A2_RC234.Text.Trim() + "', '" + this.A2_RC234Ver.Text.Trim() + "',"
                        + " '" + this.A2_RC234No.Text.Trim() + "', '" + this.A2_RD026.Text.Trim() + "', "
                        + " '" + this.A2_Run.Text.Trim() + "', '" + this.A2_Stop.Text.Trim() + "',"                        
                        + " '" + this.R_RC234.Text.Trim() + "', '" + this.R_RC234Ver.Text.Trim() + "',"
                        + " '" + this.R_RC234No.Text.Trim() + "', '" + this.Rot_RD026.Text.Trim() + "',"
                        + " '" + this.Rot_Run.Text.Trim() + "', '" + this.Rot_Stop.Text.Trim() + "',"
                        + " '" + this.Z_RC234.Text.Trim() + "', '" + this.Z_RC234Ver.Text.Trim() + "',"
                        + " '" + this.Z_RC234No.Text.Trim() + "', '" + this.Z_RD026.Text.Trim() + "',"
                        + " '" + this.Z_Run.Text.Trim() + "', '" + this.Z_Stop.Text.Trim() + "',"
                        + " '" + this.RC204.Text.Trim() + "', '" + this.RC204Ver.Text.Trim() + "',"
                        + " '" + this.RC204No.Text.Trim() + "', '" + this.A1_R022A.Text.Trim() + "',"
                        + " '" + this.A2_R022A.Text.Trim() + "', NULLIF('" + this.A1_OH.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A1_OL.Text.Trim() + "', ''), NULLIF('" + this.A1_Q.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A1_OX.Text.Trim() + "', ''), NULLIF('" + this.A1_OC.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A1_OS.Text.Trim() + "', ''), NULLIF('" + this.A1_0S.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A2_OH.Text.Trim() + "', ''), NULLIF('" + this.A2_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A2_Q.Text.Trim() + "', ''), NULLIF('" + this.A2_OX.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A2_OC.Text.Trim() + "', ''), NULLIF('" + this.A2_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.A2_0S.Text.Trim() + "', ''), NULLIF('" + this.R_OH.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.R_OL.Text.Trim() + "', ''), NULLIF('" + this.R_Q.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.R_OX.Text.Trim() + "', ''), NULLIF('" + this.R_OC.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.R_OS.Text.Trim() + "', ''), NULLIF('" + this.R_0S.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_OH.Text.Trim() + "', ''), NULLIF('" + this.Z_OL.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_Q.Text.Trim() + "', ''), NULLIF('" + this.Z_OX.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_OC.Text.Trim() + "', ''), NULLIF('" + this.Z_OS.Text.Trim() + "', ''),"
                        + " NULLIF('" + this.Z_0S.Text.Trim() + "', ''), '" + this.Remark.Text.Trim() + "')";
                    sql_cmd.Parameters.Add("@date", SqlDbType.DateTime).Value = date.Date;
                    sql_cmd.ExecuteNonQuery();
                    sql_cmd.Dispose();
                    myConnection.Close();
                    MessageBox.Show("Database updated successfully!");
                    this.Dispose();
                }
                catch (Exception ex)
                {
                    if (myConnection.State == ConnectionState.Open)
                    {
                        myConnection.Close();
                    }
                    MessageBox.Show(ex.Message);
                }                
            }
            else
            {
                MessageBox.Show(error);
            }
        }      
    }
}
